//
//  Tag.m
//  Bespoke
//
//  Created by Cheng on 12/8/2.
//
//

#import "Tag.h"
#import "AccountInitialization.h"
#import "AccountTag.h"

#define LEFT_BOUNDARY 17
#define RIGHT_BOUNDARY 17
#define BORDER_WIDTH 10
#define TOP_BOUNDARY 20
#define BORDER_HEIGHT 10
#define BOTTOM_SPACE 10

@implementation Tag
@synthesize Array_Tag, IVw_Background;
@synthesize delegate;

- (int)OptionArray:(NSMutableArray *)TagArray
{
    
    int X_axis = LEFT_BOUNDARY;
    int Y_axis = TOP_BOUNDARY;
    
    self.Array_Tag = [[NSMutableArray alloc] initWithArray:TagArray];
    
    for(AccountTag *tag in self.Array_Tag) {
        
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:tag.Tag_size];
        CGSize msgSize = [tag.Tag_Name sizeWithFont:font];
        
        UIButton *tagLabelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tagLabelButton.frame = CGRectMake(X_axis,Y_axis, msgSize.width, msgSize.height);
        
        tagLabelButton.tag = [self.Array_Tag indexOfObject:tag];
        
        [tagLabelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [tagLabelButton setTitle:tag.Tag_Name forState:UIControlStateNormal];
        
        [tagLabelButton addTarget:self action:@selector(tagbuttonPress:) forControlEvents:UIControlEventTouchUpInside];
        [tagLabelButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
        [tagLabelButton.titleLabel setFont:font];
        
        [self addSubview:tagLabelButton];
        
        CGSize msgSize_Next;
        
        //Check Last Index.
        if ([self.Array_Tag indexOfObject:tag]==self.Array_Tag.count-1) {
            
            Y_axis = Y_axis + BORDER_HEIGHT + msgSize.height;
            msgSize_Next.width = 0;
        }
        else {
            
            AccountTag *Tag_Next = [self.Array_Tag objectAtIndex:[self.Array_Tag indexOfObject:tag]+1];
            UIFont *font_Next = [UIFont fontWithName:@"Helvetica" size:Tag_Next.Tag_size];
            msgSize_Next = [Tag_Next.Tag_Name sizeWithFont:font_Next];
        }
        
        if (X_axis+msgSize.width+BORDER_HEIGHT+msgSize_Next.width<self.frame.size.width-RIGHT_BOUNDARY)
            X_axis = X_axis + BORDER_WIDTH + msgSize.width;
        else {
            X_axis = LEFT_BOUNDARY;
            Y_axis = Y_axis + BORDER_HEIGHT + msgSize.height;
        }
    }
    
    if (Y_axis+BOTTOM_SPACE>220)
        IVw_Background.image = [UIImage imageNamed:@"tag_dropdown_8lines.png"];
    else if (Y_axis+BOTTOM_SPACE>180)
        IVw_Background.image = [UIImage imageNamed:@"tag_dropdown_6lines.png"];
    else if (Y_axis+BOTTOM_SPACE>100)
        IVw_Background.image = [UIImage imageNamed:@"tag_dropdown_4lines.png"];
    else
        IVw_Background.image = [UIImage imageNamed:@"tag_dropdown_2lines.png"];

    //Minimum.
    if (Y_axis+BOTTOM_SPACE<50)
        Y_axis = 50;

    return Y_axis+BOTTOM_SPACE;
}

-(void)tagbuttonPress:(id)sender
{
    AccountTag *LoadTag = [self.Array_Tag objectAtIndex:[sender tag]];
    [delegate uiViewTouched_Tag:NO ];
    [delegate DidSelected_Tag:LoadTag.Tag_Name];
}

#pragma mark - Touches
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        [delegate uiViewTouched_Tag:YES ];
        return YES;
    }
    
    [delegate uiViewTouched_Tag:NO ];
    return NO;
}

@end
